package com.ruoyi.baidu.controller;


import com.ruoyi.baidu.service.AcaExperimentRepository;
import com.ruoyi.baidu.service.AiExperimentRepository;
import com.ruoyi.baidu.service.AlgorithmTypeRepository;
import com.ruoyi.baidu.service.AnimalRepository;
import com.ruoyi.baidu.service.impl.AcaExperimentService;
import com.ruoyi.baidu.service.impl.AiExperimentService;
import com.ruoyi.baidu.service.impl.AlgorithmTypeService;
import com.ruoyi.baidu.service.impl.AnimalService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.RemoteAcaExperimentService;
import com.ruoyi.system.api.RemoteAiExperimentService;
import com.ruoyi.system.api.RemoteAnimalInfomationService;
import com.ruoyi.system.api.domain.AiExperiment;
import com.ruoyi.system.api.domain.AlgorithmType;
import com.ruoyi.system.api.domain.AnimalInformation;
import com.ruoyi.system.api.domain.Experiment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/baidu")
public class ElasticsearchController {

    @Value("${spring.elasticsearch.page}")
    private int page;
    @Value("${spring.elasticsearch.size}")
    private int size;


    @Autowired
    private AiExperimentService aiExperimentService;
    @Autowired
    private AlgorithmTypeService algorithmTypeService;

    @Autowired
    private AcaExperimentService acaExperimentService;

    @Autowired
    private AnimalService animalService;
    
    @Autowired
    private AiExperimentRepository aiExperimentRepository;
    @Autowired
    private AlgorithmTypeRepository algorithmTypeRepository;

    @Autowired
    private AcaExperimentRepository acaExperimentRepository;

    @Autowired
    private AnimalRepository animalRepository;

    @Autowired
    private RemoteAiExperimentService remoteAiExperimentService;

    @Autowired
    private RemoteAcaExperimentService remoteAcaExperimentService;

    @Autowired
    private RemoteAnimalInfomationService remoteAnimalInfomationService;
    @GetMapping("/all")
    public Map<String,Iterable<?>> saveAll() {
        Map<String, Iterable<?>> resultMap = new HashMap<>();
        Iterable<AiExperiment> aiExperiments = aiExperimentService.findAll();
        Iterable<AlgorithmType> algorithmTypes = algorithmTypeService.findAll();
        Iterable<Experiment> acaExperiments = acaExperimentService.findAll();
        Iterable<AnimalInformation> animalInformations = animalService.findAll();
        resultMap.put("experiments", aiExperiments);
        resultMap.put("algorithms", algorithmTypes);
        resultMap.put("acaexperiments", acaExperiments);
        resultMap.put("animals", animalInformations);
        return resultMap;
    }

    @GetMapping("/delete")
    public void deleteAll() {
        aiExperimentService.deleteAll();
        algorithmTypeService.deleteAll();
        acaExperimentService.deleteAll();
        animalService.deleteAll();
    }

    @GetMapping("/save")
    public String findAll() {
        List<AiExperiment> aiExperimentList = remoteAiExperimentService.AiExperimentlist(new AiExperiment());
        List<AlgorithmType> algorithmTypeList = remoteAiExperimentService.AlgorithmTypelist(new AlgorithmType());
        List<Experiment> acaExperimentList = remoteAcaExperimentService.list1(new Experiment());
        List<AnimalInformation> animalInformationsList = remoteAnimalInfomationService.list1(new AnimalInformation());
        aiExperimentRepository.saveAll(aiExperimentList);
        algorithmTypeRepository.saveAll(algorithmTypeList);
        acaExperimentRepository.saveAll(acaExperimentList);
        animalRepository.saveAll(animalInformationsList);
        return "成功";
    }
    @GetMapping("/find")
    public AjaxResult globalSearch(@RequestParam String keyword) {
        try {
            // URL解码
            String decodedKeyword = URLDecoder.decode(keyword, StandardCharsets.UTF_8.name());
            
            Map<String,List<?>> resultMap = new HashMap<>();
            // 搜索实验数据
            List<AlgorithmType> algorithmList = algorithmTypeService.search(decodedKeyword);
            List<AiExperiment> experimentList = aiExperimentService.search(decodedKeyword);
            List<Experiment> acaExperimentList = acaExperimentService.search(decodedKeyword);
            List<AnimalInformation> animalList = animalService.search(decodedKeyword);
            if (experimentList.size()!=0 ){
                resultMap.put("experiments", experimentList);
            }
            if (algorithmList.size()!=0){
                resultMap.put("algorithms", algorithmList);
            }
            if (acaExperimentList.size()!=0){
                resultMap.put("acaexperiments", acaExperimentList);
            }
            if (animalList.size()!=0){
                resultMap.put("animals", animalList);
            }
            return AjaxResult.success(resultMap);
        } catch (Exception e) {
            return AjaxResult.error("搜索失败：" + e.getMessage());
        }
    }

    /*@GetMapping("/find1")
    public void testRemoteCall() {
        AiExperiment aiExperiment = new AiExperiment();
        aiExperiment.setExperimentId(4l);
        List<AiExperiment> experiments = remoteAiExperimentService.AiExperimentlist(aiExperiment);
        if (experiments != null) {
            experiments.forEach(System.out::println);
        } else {
            System.out.println("未获取到实验数据");
        }
    }*/
}